﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using MVVM.Model;

namespace MVVM.Repository {
    public class CustomerRepository {

        IUnitOfWork service;

        //Default contructor that creates a new istance of the unit of work
        public CustomerRepository() {
            if (this.service == null) {
                this.service = new UnitOfWork();
            }
        }
        //Constructor that gets an istance of unit of work
        public CustomerRepository(IUnitOfWork _service) {
            this.service = _service;
        }
        //Current list of Customers
        public IList<Customer> GetAllCustomers() {
            return service.GetAll<Customer>();
        }
        //Customer by query
        public IList<Customer> GetCustomersByQuery(Expression<Func<Customer, bool>> query) {
            return service.GetByQuery<Customer>(query).ToList();
        }
        //Retreive a customer by its Id
        public Customer GetCurrentCustomer(int _id) {
            return service.GetById<Customer>(c => c.CustomerID == _id);
        }
        //Add a new customer
        public void AddCustomer(Customer _customer) {
            service.Add<Customer>(_customer);
        }
        //Delete a customer
        public void DeleteCustomer(Customer _customer) {
            service.Delete<Customer>(_customer);
        }
        //Update a customer
        public void UpdateCustomer(Customer _customer) {
            service.Update<Customer>(_customer);
        }
        //Commit all changes
        public void Commit() {
            service.Commit();
        }
    }
}
